package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanshi.client.ProductsClient;
import com.wanshi.entity.Orders;
import com.wanshi.entity.Products;
import com.wanshi.entity.User;
import com.wanshi.mapper.OrdersMapper;
import com.wanshi.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private ProductsClient productsClient;

    //1 根据id查询订单
    @GetMapping("/{id}")
    public Orders findById(@PathVariable String id){
        Orders order = ordersService.getById(id);

        List<Products> list = productsClient.findproductsAndNumByorderId(id);
        order.setProds(list);
        return order;
    }

    //2 查询所有订单
    @GetMapping("/list")
    public List<Orders> list() {
        return ordersService.list();
    }

    //3 根据用户id查询订单
    @GetMapping("/user/{userid}")
    public List<Orders> findByUserId(@PathVariable String userid) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userid);
        return ordersService.list(wrapper);
    }


    //4 新增订单
    @PostMapping
    public String addOrser(@RequestBody Orders orders) {
        ordersService.save(orders);
        return "添加成功";
    }



}

